package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.enums.ConsumptionEnums;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.service.*;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IEmployeeService employeeService;


    @RequiredPermission(name="结算单页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("enums", ConsumptionEnums.values());
        model.addAttribute("business",businessService.listAll());
        return "consumption/list";
    }


    @RequiredPermission(name="结算单删除",expression="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
           consumptionItemService.deleteAll(id);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        model.addAttribute("businesses",businessService.listAll());
//       共享业务类
        model.addAttribute("sysitems",systemDictionaryItemService.selectItems("business"));
        model.addAttribute("payitems",systemDictionaryItemService.selectItems("pay_type"));
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            if (consumption.getPayeeId()!=null){
//                结算人共享
                Employee employee=employeeService.getEmployeeName(consumption.getPayeeId().getId());
                model.addAttribute("e",employee);
            }

            if (consumption.getAuditorId()!=null){
//               审核人共享
                Employee employee=employeeService.getEmployeeName(consumption.getAuditorId().getId());
                model.addAttribute("eaduit",employee);
            }
            model.addAttribute("consumption", consumption);
            model.addAttribute("items",consumptionItemService.selectItems(id));
        }
        return "consumption/input";
    }

    @RequestMapping("/save")
    public String save(Long appointmentId){
       Long  consumptionId = consumptionService.saveAppoiment(appointmentId);
       return   "redirect:/consumption/input?id="+consumptionId;
    }

    @RequestMapping("/payFinal")
    public String payFinal(Long id){
         consumptionService.payFianl(id);
       return   "redirect:/consumption/input?id="+id;
    }
//    审核
    @RequestMapping("/audit")
    public String audit(Long id,Integer status){
         consumptionService.auditById(id,status);
       return   "redirect:/consumption/input?id="+id;
    }

    @RequestMapping("/inputSysItems")
    @ResponseBody
    public List<SystemDictionaryItem> inputSysItems(Long typeId){
        return systemDictionaryItemService.listAll(typeId);
    }
//
//    @RequestMapping("/audit")
//    @ResponseBody
//    public Consumption audit(Long id){
//        Consumption consumption = consumptionService.get(id);
//        return consumption;
//    }



}
